package org.etom.impl.ui.wizards;

import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.DirectoryDialog;

public class DownloadAsNewProjectPage extends AbstractDownloadAsNewProjectPage
                                      implements SelectionListener {

  @Override
  public void createControl(Composite parent) {
    super.createControl(parent);
    addListeners();
    initPage();
  }
  
  private void addListeners() {
    useDefaultLocationButton.addSelectionListener(this);
    browseButton.addSelectionListener(this);
  }
  
  //===[Selection Listener]===//
  @Override
  public void widgetSelected(SelectionEvent e) {
    if (e.getSource() == useDefaultLocationButton) {
      useDefaultButtonSelected();
    } else if (e.getSource() == browseButton) {
      browseButtonSelected();
    }
  }
  
  @Override
  public void widgetDefaultSelected(SelectionEvent e) {
    //nothing to do here
  }
  //===[Selection Listener]===//
  
  public String getProjectLocation() {
    return useDefaultLocationButton.getSelection() ?
        null : locationText.getText();
  }
  
  private void initPage() {
    useDefaultLocationButton.setSelection(true);
    useDefaultButtonSelected();
  }
  
  private void useDefaultButtonSelected() {
    boolean selected = useDefaultLocationButton.getSelection();
    locationText.setEnabled(!selected);
    browseButton.setEnabled(!selected);
  }
  
  private void browseButtonSelected() {
    DirectoryDialog dirDialog = new DirectoryDialog(browseButton.getShell());
    String path = dirDialog.open();
    if (path == null) return;
    locationText.setText(path);
  }
  
}
